package frontend;
import framework.*;
import dbmanager.*;

import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTextPane;

import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.SwingUtilities;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class LoginGUI extends javax.swing.JFrame {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JButton loginButton;
	private JPasswordField passwordField;
	private JPanel imagePanel;
	private JLabel errorLabel;
	private JLabel newAccountLabel;
	private JLabel passwordLabel;
	private JLabel imageLabel;
	private JFrame imageFrame;
	private JLabel statusLabel;
	private JTextField usernameField;
	private boolean loginGUIOpen = false;

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				LoginGUI inst = new LoginGUI();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public LoginGUI() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			DatabaseObjectManager.connect();
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			AnchorLayout thisLayout = new AnchorLayout();
			getContentPane().setLayout(thisLayout);
			getContentPane().setBackground(new java.awt.Color(255,255,255));
			this.setResizable(false);
			this.setTitle("NOM");
			{
				errorLabel = new JLabel();
				getContentPane().add(errorLabel, new AnchorConstraint(787, 949, 833, 46, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				errorLabel.setPreferredSize(new java.awt.Dimension(241, 23));
			}
			this.addWindowListener(new WindowAdapter() {
				public void windowClosed(WindowEvent evt) {
					thisWindowClosed(evt);
				}
			});
			{
				newAccountLabel = new JLabel();
				getContentPane().add(newAccountLabel, new AnchorConstraint(728, 833, 758, 46, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				newAccountLabel.setText("<html><u>Don't have a NOM user account?</u></html>");
				newAccountLabel.setPreferredSize(new java.awt.Dimension(210, 15));
				newAccountLabel.setFont(new java.awt.Font("AlArabiya",0,13));
				newAccountLabel.setForeground(new java.awt.Color(56,0,255));
				newAccountLabel.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						newAccountLabelMouseClicked(evt);
					}
				});
			}
			{
				passwordLabel = new JLabel();
				getContentPane().add(passwordLabel, new AnchorConstraint(584, 387, 622, 76, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				passwordLabel.setText("Password");
				passwordLabel.setPreferredSize(new java.awt.Dimension(83, 19));
				passwordLabel.setFont(new java.awt.Font("AlArabiya",1,13));
			}
			{
				imageLabel = new JLabel(createImageIcon("nom.jpg","om nom nom nom"));
				getContentPane().add(imageLabel, new AnchorConstraint(18, 949, 367, 58, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				imageLabel.setPreferredSize(new java.awt.Dimension(238, 177));
			}
			{
				statusLabel = new JLabel();
				getContentPane().add(statusLabel, new AnchorConstraint(423, 949, 458, 76, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				statusLabel.setText("NOM Username");
				statusLabel.setPreferredSize(new java.awt.Dimension(233, 18));
				statusLabel.setFont(new java.awt.Font("AlArabiya",1,13));
			}
			{
				passwordField = new JPasswordField();
				getContentPane().add(passwordField, new AnchorConstraint(634, 956, 693, 46, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				passwordField.setPreferredSize(new java.awt.Dimension(243, 30));
				passwordField.addKeyListener(new KeyAdapter() {
					public void keyPressed(KeyEvent evt) {
						try {
							passwordFieldKeyPressed(evt);
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});
			}
			{
				usernameField = new JTextField();
				getContentPane().add(usernameField, new AnchorConstraint(470, 956, 527, 46, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				usernameField.setPreferredSize(new java.awt.Dimension(243, 29));
			}
			{
				loginButton = new JButton();
				getContentPane().add(loginButton, new AnchorConstraint(898, 713, 959, 297, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				loginButton.setText("Sign in");
				loginButton.setPreferredSize(new java.awt.Dimension(111, 31));
				loginButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						try {
							loginButtonActionPerformed(evt);
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});
			}
			pack();
			this.setSize(275, 535);
		} catch (Exception e) {
			e.printStackTrace();
		} 	
	}
	
	public void setUsername(String username){
		usernameField.setText(username);
	}
	
	
	private void loginButtonActionPerformed(ActionEvent evt) throws SQLException {
		// check login
		login(usernameField.getText(), new String(passwordField.getPassword()));
		
		
	}
	
	private void login(String username, String password) throws SQLException{
		if(!AccountManager.login(username, password)){
			errorLabel.setText("Invalid username or password!");
			return;
		} else {
			this.setVisible(false);
			NomGUI inst = new NomGUI();
			inst.setLocationRelativeTo(null);
			inst.setVisible(true);
		}
		
	}
	
	protected ImageIcon createImageIcon(String path, String description) {
		java.net.URL imgURL = getClass().getResource(path);
		if (imgURL != null) {
			return new ImageIcon(imgURL, description);
		} else {
			System.err.println("Couldn't find file: " + path);
			return null;
		}
	}
	
	public void setLoginGUIOpen(boolean status){
		this.loginGUIOpen = status;
	}
	
	private void newAccountLabelMouseClicked(MouseEvent evt) {
		if(loginGUIOpen) return;
		
		loginGUIOpen = true;
		CreateUserGUI inst = new CreateUserGUI(this);
		inst.setLocationRelativeTo(null);
		inst.setVisible(true);
	}
	
	private void thisWindowClosed(WindowEvent evt) {
		System.exit(0);
	}
	
	private void passwordFieldKeyPressed(KeyEvent evt) throws SQLException {
		if(evt.getKeyCode() == KeyEvent.VK_ENTER){
			login(usernameField.getText(), new String(passwordField.getPassword()));
		}
		//TODO add your code for passwordField.keyPressed
	}

}
